IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'FILTER_FIELD')
BEGIN
    DROP TABLE [dbo].[FILTER_FIELD]
END

GO

CREATE TABLE [dbo].[FILTER_FIELD] ( 
	[ID]			    INT IDENTITY(1,1)   NOT FOR REPLICATION     NOT NULL,
    [FILTER_ID]         INT                 NOT NULL,
    [FIELD_NAME]        VARCHAR(50)         NOT NULL,
    [TABLE_NAME]       	VARCHAR(50)         NOT NULL,
    [COLUMN_NAME]       VARCHAR(50)         NOT NULL,
    [TYPE]              VARCHAR(10)         NOT NULL CONSTRAINT [CK_FILTER_FIELD_TYPE]
            CHECK NOT FOR REPLICATION (TYPE IN ('Text', 'Number', 'Date', 'Select', 'Check'))
            DEFAULT 'Text',
    [SORTED]            VARCHAR(10)         NOT NULL CONSTRAINT [CK_FILTER_FIELD_SORTED]
            CHECK NOT FOR REPLICATION (SORTED IN ('Non', 'Asc', 'Desc'))
            DEFAULT 'Non',
    [VALUE]             VARCHAR(256) 

    CONSTRAINT [PK_FILTER_FIELD_ID] PRIMARY KEY CLUSTERED ( 
       	[ID] 
    ) ON [PRIMARY],
    
    CONSTRAINT [FK_FILTER_FIELD_FILTER_ID] FOREIGN KEY ( 
        [FILTER_ID]
    ) REFERENCES [dbo].[FILTER] (
        [ID]
    )
    
) ON [PRIMARY] 

CREATE INDEX [IDX_FILTER_FIELD_FILTER_ID] on [dbo].[FILTER_FIELD] ([FILTER_ID])

GO